﻿<%@ Page Language="C#" AutoEventWireup="true" Inherits="System.Web.UI.Page" %>
<%@ Import Namespace="Portal.Data.Entity" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Update" OnClick="OnUpdate"/>
        <asp:Button ID="Button2" runat="server" Text="Move none Author" OnClick="OnUpdateAuthor"/>
    </div>
    </form>
</body>
</html>



<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void OnUpdate(object sender, EventArgs e)
    {
        //        const string path = "Admin/Config/";
        //        var list = Portal.Data.Entity.Page.ListAll();
        //        foreach (Page page in list)
        //        {
        //            string sContent = MyFile.ReadTextFromFile(CoreContext.absStaticFile+ path + page.Name + ".config");
        //            page.Content = sContent;
        //            ORMHelper.InstanceIgnorePermission.Save(page);
        //        }
    }

    protected void OnUpdateAuthor(object sender, EventArgs e)
    {
        const int noneAuthId = 137;
        using (var context = new CmsDataContext())
        {
            var bIds = context.Books.Where(x => x.AuthorId == 0).Select(x => x.ID).ToArray();
            if (bIds.Any())
            {
                var sbid = string.Join(",", bIds);
                var oldIds = context.BookByAuthors.Where(x => x.AuthorId == noneAuthId).Select(x => x.BookIds).FirstOrDefault();
                if (oldIds == null)
                {
                    context.ExecuteCommand("INSERT INTO BookByAuthor(AuthorId,BookIds) VALUES ({0},{1})", noneAuthId, sbid);
                }
                else
                {
                    oldIds += "," + sbid;
                    context.ExecuteCommand("UPDATE BookByAuthor SET BookIds={0} WHERE AuthorId={1}", oldIds, noneAuthId);
                }
                
                //update 
                context.ExecuteCommand("UPDATE Book SET AuthorId={0} WHERE AuthorId=0", noneAuthId);
            }
        }
    }
</script>